home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Aminet 24
/
Aminet 24 (1998)(GTI - Schatztruhe)[!][Apr 1998].iso
/
Aminet
/
dev
/
c
/
cxref_1_4a.lha
/
xref.c
< prev
Wrap
C/C++ Source or Header
|
1997-12-07
|
23KB
|
874 lines
/***************************************
$Header: /home/amb/cxref/RCS/xref.c 1.15 1997/05/18 10:05:52 amb Exp $
C Cross Referencing & Documentation tool. Version 1.4.
Cross referencing of functions.
******************/ /******************
Written by Andrew M. Bishop
This file Copyright 1995,96,97 Andrew M. Bishop
It may be distributed under the GNU Public License, version 2, or
any higher version. See section COPYING of the GNU Public license
for conditions under which this file may be redistributed.
***************************************/
/*+ The names of the function cross reference files. +*/
#define XREF_FUNC_FILE ".function"
#define XREF_FUNC_BACKUP ".function~"
/*+ The names of the variable cross reference files. +*/
#define XREF_VAR_FILE ".variable"
#define XREF_VAR_BACKUP ".variable~"
/*+ The names of the include cross reference files. +*/
#define XREF_INC_FILE ".include"
#define XREF_INC_BACKUP ".include~"
/*+ The names of the type cross reference files. +*/
#define XREF_TYPE_FILE ".typedef"
#define XREF_TYPE_BACKUP ".typedef~"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#ifdef AMIGA /* olsen */
#include "amiga.h"
#endif /* AMIGA */
#include "memory.h"
#include "datatype.h"
#include "cxref.h"
/*+ The name of the directory for the output. +*/
extern char* option_odir;
/*+ The base name of the file for the output. +*/
extern char* option_name;
/*+ The option for cross referencing. +*/
extern int option_xref;
/*+ The option for indexing. +*/
extern int option_index;
static void check_for_called(File file,char* called,char* caller,char* filename);
static void check_for_caller(File file,char* called,char* filename);
static void check_for_var(File file,char* variable,char* filename,int scope,char* funcname);
static int check_for_var_func(File file,Variable var,Function func);
static void fixup_extern_var(Variable var,StringList2 refs);
/*++++++++++++++++++++++++++++++++++++++
Cross reference the functions, variables and includes that are used in this file
with the global functions, variables and includes. The types that are defined are also listed here.
File file The file structure containing the information.
++++++++++++++++++++++++++++++++++++++*/
void CrossReference(File file)
{
FILE *in,*out;
char *ifile,*ofile;
/* Format: filename [[%]include1] [[%]include2] ... : Files include1, include2, ... are included in filename;
those with a % are local. */
if(option_xref&XREF_FILE) /* First do the files */
{
Include inc;
ifile=ConcatStrings(4,option_odir,"/",option_name,XREF_INC_FILE);
ofile=ConcatStrings(4,option_odir,"/",option_name,XREF_INC_BACKUP);
in =fopen(ifile,"r");
out=fopen(ofile,"w");
if(!out)
{fprintf(stderr,"cxref: Failed to open the include cross reference file '%s'\n",ofile);exit(1);}
fprintf(out,"%s",file->name);
for(inc=file->includes;inc;inc=inc->next)
fprintf(out," %s%s",inc->scope==LOCAL?"%":"",inc->name);
fprintf(out,"\n");
if(in)
{
char include[128],filename[128],ch;
while(fscanf(in,"%s%c",filename,&ch)==2)
{
int diff_file=strcmp(filename,file->name);
if(diff_file)
fprintf(out,"%s",filename);
while(ch==' ')
{
fscanf(in,"%s%c",include,&ch);
if(diff_file)
fprintf(out," %s",include);
if(include[0]=='%' && !strcmp(&include[1],file->name))
AddToStringList(file->inc_in,filename,1,1);
}
if(diff_file)
fprintf(out,"\n");
}
fclose(in);
unlink(ifile);
}
fclose(out);
rename(ofile,ifile);
}
/* Format: filename funcname scope [[%][&]funcname1] [[%][&]funcname2] ... : The function funcname in file filename
calls or references functions funcname1, funcname2 ... ; those with a % are local, with a & are references. */
/* Format: filename $ 0 [[%]&funcname1] [[%]&funcname2] ... : The file references functions funcname1, funcname2 ... ;
those with a % are local. */
if(option_xref&XREF_FUNC) /* Now do the functions */
{
Function func;
int i;
ifile=ConcatStrings(4,option_odir,"/",option_name,XREF_FUNC_FILE);
ofile=ConcatStrings(4,option_odir,"/",option_name,XREF_FUNC_BACKUP);
in =fopen(ifile,"r");
out=fopen(ofile,"w");
if(!out)
{fprintf(stderr,"cxref: Failed to open the functional cross reference file '%s'\n",ofile);exit(1);}
for(i=0;i<file->f_refs->n;i++)
check_for_called(file,ConcatStrings(2,"&",file->f_refs->s1[i]),NULL,file->name);
for(func=file->functions;func;func=func->next)
{
for(i=0;i<func->calls->n;i++)
check_for_called(file,func->calls->s1[i],func->name,file->name);
for(i=0;i<func->f_refs->n;i++)
check_for_called(file,ConcatStrings(2,"&",func->f_refs->s1[i]),func->name,file->name);
}
for(func=file->functions;func;func=func->next)
check_for_caller(file,func->name,file->name);
if(file->f_refs->n)
{
fprintf(out,"%s $ 0",file->name);
for(i=0;i<file->f_refs->n;i++)
{
if(file->f_refs->s2[i])
fprintf(out," %%&%s",file->f_refs->s1[i]);
else
fprintf(out," &%s",file->f_refs->s1[i]);
}
fprintf(out,"\n");
}
for(func=file->functions;func;func=func->next)
{
fprintf(out,"%s %s %d",file->name,func->name,func->scope);
for(i=0;i<func->calls->n;i++)
{
if(func->calls->s2[i])
fprintf(out," %%%s",func->calls->s1[i]);
else
fprintf(out," %s",func->calls->s1[i]);
}
for(i=0;i<func->f_refs->n;i++)
{
if(func->f_refs->s2[i])
fprintf(out," %%&%s",func->f_refs->s1[i]);
else
fprintf(out," &%s",func->f_refs->s1[i]);
}
fprintf(out,"\n");
}
if(in)
{
char ch,funcname[64],filename[128],called[64];
int scope;
while(fscanf(in,"%s %s %d%c",filename,funcname,&scope,&ch)==4)
{
int diff_file=strcmp(filename,file->name);
if(diff_file)
{
if(funcname[0]!='$')
check_for_caller(file,funcname,filename);
fprintf(out,"%s %s %d",filename,funcname,scope);
}
while(ch==' ')
{
fscanf(in,"%s%c",called,&ch);
if(diff_file)
{
if(called[0]!='%')
check_for_called(file,called,funcname[0]=='$'?NULL:funcname,filename);
fprintf(out," %s",called);
}
}
if(diff_file)
fprintf(out,"\n");
}
fclose(in);
unlink(ifile);
}
fclose(out);
rename(ofile,ifile);
}
/* Format: filename varname scope [$] [[%]funcname1] [[%]funcname2] ... : variable varname is used in
the file filename if $, and functions funcname1, funcname2 ... Those with a % are local. */
if(option_xref&XREF_VAR) /* Now do the variables */
{
Variable var;
Function func;
ifile=ConcatStrings(4,option_odir,"/",option_name,XREF_VAR_FILE);
ofile=ConcatStrings(4,option_odir,"/",option_name,XREF_VAR_BACKUP);
in =fopen(ifile,"r");
out=fopen(ofile,"w");
if(!out)
{fprintf(stderr,"cxref: Failed to open the variable cross reference file '%s'\n",ofile);exit(1);}
for(var=file->variables;var;var=var->next)
{
check_for_var(file,var->name,file->name,var->scope,NULL);
fprintf(out,"%s %s %d",file->name,var->name,var->scope);
if(check_for_var_func(file,var,NULL))
fprintf(out," $");
for(func=file->functions;func;func=func->next)
if(check_for_var_func(file,var,func))
fprintf(out," %s%s",func->scope==LOCAL?"%":"",func->name);
fprintf(out,"\n");
}
if(in)
{
char varname[64],filename[128],funcname[64],ch;
int scope;
while(fscanf(in,"%s %s %d%c",filename,varname,&scope,&ch)==4)